package web.PetStore.web.servlet;

import web.PetStore.domain.Account;
import web.PetStore.service.AccountService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(name = "/AccountServlet", urlPatterns = {"/Account"})
public class AccountServlet extends HttpServlet {
    private String oid, userid, password, newpassword, phone, address, email;
    private boolean changePassword;
    private AccountService accountService = new AccountService();

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setCharacterEncoding("utf-8");
        this.oid = ((Account) req.getSession().getAttribute("loginUser")).getUserid();
        this.userid = req.getParameter("userid");
        this.password = req.getParameter("password");
        this.newpassword = req.getParameter("newpassword");
        this.phone = req.getParameter("phone");
        this.address = req.getParameter("address");
        this.email = req.getParameter("email");
        changePassword = !password.isEmpty();
        if(userid == null || userid.equals("")){
            req.setAttribute("accountInfo", "用户名不能为空");
            req.getRequestDispatcher("AccountForm").forward(req,resp);
            return;
        }else if(!oid.equals(userid) && accountService.findUserById(userid)){
            req.setAttribute("accountInfo", "用户名已存在");
            req.getRequestDispatcher("AccountForm").forward(req,resp);
            return;
        }else if(phone == null || phone.equals("")){
            req.setAttribute("accountInfo", "手机号码不能为空");
            req.getRequestDispatcher("AccountForm").forward(req,resp);
            return;
        }else if(changePassword){
            Account account = accountService.confirmUserByIdAndPassword(oid,password);
            if(account == null){
                req.setAttribute("accountInfo", "原密码错误");
                req.getRequestDispatcher("AccountForm").forward(req,resp);
                return;
            }else if(password.equals(newpassword)){
                req.setAttribute("accountInfo", "新密码与原密码不能相同");
                req.getRequestDispatcher("AccountForm").forward(req,resp);
                return;
            }
        }
        Account user = new Account();
        user.setUserid(userid);
        user.setPassword(password);
        user.setPhone(phone);
        user.setEmail(email);
        user.setAddress(address);
        if(accountService.updateUser((changePassword ? newpassword : null), user)){
            req.getSession().setAttribute("loginUser", user);
            req.getRequestDispatcher("index.jsp").forward(req,resp);
        }else{
            req.setAttribute("accountInfo", "更新用户信息失败");
            req.getRequestDispatcher("AccountForm").forward(req,resp);
        }
    }
}
